package com.youlu.campus.service.recycle;

import com.google.common.collect.Lists;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.common.utils.IdGeneratorUtils;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.WechatMerchantConfig;
import com.youlu.campus.entity.recycle.RecycleItemInfo;
import com.youlu.campus.entity.recycle.RecycleOrderInfo;
import com.youlu.campus.entity.recycle.dto.RecycleItemSearchDTO;
import com.youlu.campus.entity.recycle.dto.RecycleMyOrderDTO;
import com.youlu.campus.entity.recycle.dto.RecycleOrderCreateDTO;
import com.youlu.campus.entity.recycle.vo.RecycleOrderInfoVo;
import com.youlu.campus.entity.recycle.vo.StoreInfoVo;
import com.youlu.campus.service.course.QueryWrapper;
import com.youlu.campus.service.course.WechatPayService;
import com.youlu.campus.service.recycle.convert.RecycleItemConvert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : [dengbo]
 * @className : RecycleMIniController
 * @description : [回收小程序控制器]
 * @createTime : [2022/6/23 16:32]
 */
@Service
@Slf4j
public class RecycleMiniService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private WechatPayService wechatPayService;
    @Autowired
    private RecycleItemConvert recycleItemConvert;

    public boolean createOrder(RecycleOrderCreateDTO dto) {
        Query query = new Query(Criteria.where("id").is(dto.getItemId()).and("onSale").is(1));
        RecycleItemInfo itemInfo = mongoTemplate.findOne(query, RecycleItemInfo.class);
        if (itemInfo == null) {
            throw new BusinessException("商品信息不存在");
        }
        RecycleOrderInfo info = new RecycleOrderInfo();
        UserPO userPO = dto.getUserPO();
        info.setPrice(itemInfo.getPrice());
        info.setUserId(dto.getUserId());
        info.setNum(dto.getNum());
        String orderNo = IdGeneratorUtils.generateRecycleNo();
        info.setOrderNo(orderNo);
        info.setTotalPrice(dto.getNum() * info.getPrice());
        info.setStatus(0);
        info.setClientIp(dto.getClientIp());
        info.setNickname(userPO.getNickName());
        info.setOpenId(userPO.getOpenId());
        info.setAvatarUrl(userPO.getAvatarUrl());
        info.setAddress(dto.getAddress());
        info.setUserName(dto.getUserName());
        info.setAppId(dto.getAppId());
        info.setPlatform(dto.getPlatform());
        info.setPhone(dto.getPhone());
        info.setArea(dto.getArea());
        info.setCity(dto.getCity());
        info.setProvince(dto.getProvince());
        info.setItemId(dto.getItemId());
        info.setRecycleStoreCode(dto.getRecycleStoreCode());
        info.setRecycleStoreName(dto.getRecycleStoreName());
        info.setRecycleType(dto.getRecycleType() == null ? 1 : dto.getRecycleType());
        // 获取商户信息
        WechatMerchantConfig merchantInfo = wechatPayService.getRandomMerchant(dto.getAppId());
        info.setMerchantId(merchantInfo.getMerchantId());
        info.setMerchantName(merchantInfo.getMerchantName());
        mongoTemplate.save(info);
        return true;
    }

    public List<StoreInfoVo> getStoreList() {
        List list = Lists.newArrayList();
        StoreInfoVo storeInfoVo = new StoreInfoVo();
        storeInfoVo.setCode("bjcy");
        storeInfoVo.setName("北京朝阳回收分店");
        list.add(storeInfoVo);
        StoreInfoVo storeInfoVo1 = new StoreInfoVo();
        storeInfoVo1.setCode("tjzb");
        storeInfoVo1.setName("天津总部回收店");
        list.add(storeInfoVo1);
        StoreInfoVo storeInfoVo2 = new StoreInfoVo();
        storeInfoVo2.setCode("bjhd");
        storeInfoVo2.setName("北京海淀回收分店");
        list.add(storeInfoVo2);
        return list;
    }

    public Page<RecycleOrderInfoVo> myOrderList(RecycleMyOrderDTO dto) {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotBlank(dto.getOrderNo())) {
            query.where("orderNo", dto.getOrderNo());
        }
        query.where("userId", dto.getUserId());
        long count = mongoTemplate.count(query, RecycleOrderInfo.class);
        query.with(Sort.by(Sort.Order.desc("id")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<RecycleOrderInfo> data = mongoTemplate.find(query.with(pageRequest), RecycleOrderInfo.class);
        List<RecycleOrderInfoVo> list = recycleItemConvert.recycleOrderToVo(data);
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    public Page<RecycleItemInfo> search(RecycleItemSearchDTO dto) {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotBlank(dto.getName())) {
            query.like("name", dto.getName());
        }
        query.addCriteria(Criteria.where("onSale").is(1));
        query.addCriteria(Criteria.where("deleted").ne(true));
        long count = mongoTemplate.count(query, RecycleItemInfo.class);
        query.with(Sort.by(Sort.Order.desc("id")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<RecycleItemInfo> data = mongoTemplate.find(query.with(pageRequest), RecycleItemInfo.class);
        return PageableExecutionUtils.getPage(data, pageRequest, () -> count);
    }
}
